﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CentrisScraper.Lib.Emails;
using Raven.Client.Linq;

namespace CentrisScraper.Lib
{
    public class Notifier
    {
        public static void SendNotificationFor(IList<Listing> listings, string tag)
        {
            using (var session = PersistenceManager.DocumentStore.OpenSession())
            {
                //var newListing = listings.Except()
                var toExclude = session.Query<Listing>()
                    .Customize(x => x.WaitForNonStaleResultsAsOfNow())
                    .Where(l => l.NoMLS.In(listings.Select(x => x.NoMLS)))
                    .Select(l => l);

                var newListing = listings.Where(l => !toExclude.Any(x => x.NoMLS == l.NoMLS && x.Prix == l.Prix));

                foreach (var listing in newListing)
                {
                        var email = new Email();
                        email.To = "samuel.coutu@gmail.com, jacinthe.meunier@gmail.com";
                        email.Subject = "CentrisScraper - " + tag + " - " + listing.Adresse + ", " + listing.Ville;
                        email.Body =
                            @"
                                <table>
                                    <tr valign='bottom'>
                                        <td>
                                            <img src='{0}'/><br>
                                            <a href='HTTP://mls.centris.ca/sommaire.aspx?noInscription={1}&lang=FR&fav='>Lien Centris</a>
                                        </td>
                                        <td>
                                            <img src='http://maps.googleapis.com/maps/api/staticmap?center={2},{3}&zoom=13&size=300x300&sensor=false&markers=color:red%7Clabel:A%7C{2},{3}'/><br>
                                            <a href='http://maps.google.com/maps?q={2},{3}'>Lien Google Map</a>
                                        </td>
                                    </tr>
                                </table>
                                <br>
                                <br>
                                <table>
                                    <tr><td>NoMLS</td><td>{4}</td></tr>
                                    <tr><td>DateAjout</td>{5}<td></td></tr>
                                    <tr><td>Prix</td><td>{6}</td></tr>
                                    <tr><td>Adresse</td><td>{7}</td></tr>
                                    <tr><td>Ville</td><td>{8}</td></tr>
                                    <tr><td>CodePostal</td>{9}<td></td></tr>
                                    <tr><td>AnneeConstruction</td>{10}<td></td></tr>
                                    <tr><td>Chambres</td><td>{11}</td></tr>
                                    <tr><td>SallesDeBain</td><td>{12}</td></tr>
                                    <tr><td>DimensionTerrain</td><td>{13}</td></tr>
                                    <tr><td>EvaluationMunicipale</td><td>{14}</td></tr>
                                    <tr><td>Agence</td><td>{15}</td></tr>
                                    <tr><td>CentrisScraper Id</td><td>{16}</td></tr>
                                </table>
                                <br>
                                <br>
                                <pre>{17}</pre>
                            ";
                        email.Body = string.Format(email.Body, listing.Image, listing.NoMLS, listing.Latitude,
                                                   listing.Longitude, listing.NoMLS, listing.DateAjout,
                                                   listing.Prix, listing.Adresse, listing.Ville, listing.CodePostal,
                                                   listing.AnneeConstruction, listing.Chambres, listing.SallesDeBain,
                                                   listing.DimensionTerrain,
                                                   listing.EvaluationMunicipale, listing.Agence, listing.Id,
                                                   listing.RawData);
                        email.Send();
                        session.Store(listing);
                }
                session.SaveChanges();
            }
        }
    }
}
